<template>
  <div>
    <el-dialog
      :title="title"
      :visible="dialogFormVisible"
      @close="closeDialog"
      @open="openDialog"
    >
      <el-form :model="form" label-width="100px">
        <el-form-item label="权限类型">
          <el-radio v-model="form.is_point" :label="false" :disabled="bool">
            菜单
          </el-radio>
          <el-radio v-model="form.is_point" :label="true" :disabled="bool">
            权限点
          </el-radio>
        </el-form-item>
        <el-form-item label="权限组名称">
          <el-select v-model="form.pid" placeholder="请选择">
            <el-option :value="null" label="主导航" />
            <el-tree
              icon-class="none"
              node-key="id"
              :data="treeData"
              :props="defaultProps"
              default-expand-all
              :expand-on-click-node="false"
            >
              <template #default="{ node, data }">
                <el-option
                  :value="data.id"
                  :label="data.title"
                  :disabled="
                    node.level === 1 && form.is_point === true ? true : false
                  "
                />
              </template>
            </el-tree>
          </el-select>
        </el-form-item>
        <el-form-item label="权限代码">
          <el-input v-model="form.code" />
        </el-form-item>
        <el-form-item label="权限标题">
          <el-input v-model="form.title" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="submitAddMenus">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addMenusApi, getMenusDetailApi, editMenusDetailApi } from '@/api/menus'
export default {
  props: {
    dialogFormVisible: Boolean,
    tableData: Array
  },
  data() {
    return {
      form: {
        title: '',
        is_point: false, // 默认选中菜单单选框
        pid: null,
        code: '',
        id: ''
      },
      defaultProps: {
        children: 'childs'
        /* label: "title", */
      },
      treeData: [],
      bool: false // 编辑时不能修改菜单和权限点
    }
  },
  computed: {
    title() {
      return this.form.id ? '编辑菜单' : '添加菜单'
    }
  },
  created() {},
  methods: {
    closeDialog() {
      this.$emit('update:dialogFormVisible', false)
      this.form = {
        title: '',
        is_point: '',
        pid: '',
        code: '',
        id: ''
      }
      this.bool = false
    },
    async openDialog(id) {
      this.treeData = this.tableData
      this.treeData.forEach((item) => {
        if (item.childs) {
          item.childs.forEach((item) => {
            if (item.childs) {
              item.childs = []
            }
          })
        }
      })
      if (id) {
        this.bool = true
        const { data } = await getMenusDetailApi(id)
        this.form = data
      }
    },
    async submitAddMenus() {
      if (this.form.id) {
        await editMenusDetailApi(this.form.id, this.form)
        this.$message.success('编辑成功')
      } else {
        await addMenusApi(this.form)
        this.$message.success('添加成功')
      }
      this.$emit('addMenus')
      this.closeDialog()
    }
  }
}
</script>

<style></style>
